﻿using homework03_2;
using System;
using System.Collections.Generic;

namespace homework03_3
{
    // 形状接口和类的定义保持不变

    // 简单工厂类
public class ShapeFactory
{
    private static Random random = new Random();

    public static IShape CreateRandomShape()
    {
        int shapeType = random.Next(3); // 0: Rectangle, 1: Square, 2: Triangle

        switch (shapeType)
        {
            case 0: // Rectangle
                double length = random.Next(1, 10) + random.NextDouble();
                double width = random.Next(1, 10) + random.NextDouble();
                return new Rectangle(length, width);

            case 1: // Square
                double side = random.Next(1, 10) + random.NextDouble();
                return new Square(side);

            case 2: // Triangle
                // 确保生成合法的三角形
                double s1, s2, s3;
                do
                {
                    s1 = random.Next(1, 10) + random.NextDouble();
                    s2 = random.Next(1, 10) + random.NextDouble();
                    s3 = random.Next(1, 10) + random.NextDouble();
                } while (!(s1 + s2 > s3 && s2 + s3 > s1 && s1 + s3 > s2));
                return new Triangle(s1, s2, s3);

            default:
                throw new ArgumentException("Invalid shape type");
        }
    }
}

// 主程序
class Program3
{
    static void Main1()
    {
        List<IShape> shapes = new List<IShape>();
        double totalArea = 0;

        // 创建10个随机形状
        for (int i = 0; i < 10; i++)
        {
            IShape shape = ShapeFactory.CreateRandomShape();
            shapes.Add(shape);
        }

        // 输出每个形状的信息并计算总面积
        Console.WriteLine("随机生成的形状及其面积：");
        for (int i = 0; i < shapes.Count; i++)
        {
            IShape shape = shapes[i];
            double area = shape.CalculateArea();
            totalArea += area;

            string shapeInfo = shape switch
            {
                Rectangle r => $"长方形 (长: {r.Length:F2}, 宽: {r.Width:F2})",
                Square s => $"正方形 (边长: {s.Side:F2})",
                Triangle t => $"三角形 (边: {t.Side1:F2}, {t.Side2:F2}, {t.Side3:F2})",
                _ => "未知形状"
            };

            Console.WriteLine($"形状 {i + 1}: {shapeInfo}, 面积: {area:F2}");
        }

        Console.WriteLine($"\n所有形状的面积总和: {totalArea:F2}");
    }
}
}
